package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jd.finance.core.mapper.UserMapper;
import com.jd.finance.core.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ASUS
 */
@Slf4j
@Service
public class AdminUtilsServiceImpl {

    @Resource
    private UserMapper userMapper;


    /**
     * 根据uid 判断是否为管理员
     *
     * @param uid
     * @return
     */
    public boolean isAdmin(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getUid, uid).eq(User::getAdmin, 1);
        Integer integer = userMapper.selectCount(qw);
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        return integer >= 1;
    }

    /**
     * 根据uid 判断是否为审核员
     *
     * @param uid
     * @return
     */
    public boolean isAuditor(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }
        if (isAdmin(uid)){
        int count = userMapper.isAuditor(uid);
        return count >= 1;
        }else{
            log.error("该用户不是管理员");
            return false;
        }
    }

    /**
     * 根据uid 判断是否为风控员
     *
     * @param uid
     * @return
     */
    public boolean isRisk(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }
        if (isAdmin(uid)){
            int count = userMapper.isRisk(uid);
            return count >= 1;
        }else{
            log.error("该用户不是管理员");
            return false;
        }
    }

    /**
     * 根据uid 判断是否为银行信贷员
     *
     * @param uid
     * @return
     */
    public boolean isBank(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }

        if (isAdmin(uid)){
            int count = userMapper.isBank(uid);
            return count >= 1;
        }else{
            log.error("该用户不是管理员");
            return false;
        }


    }

    /**
     * 根据uid 判断是否为信息管理员
     *
     * @param uid
     * @return ture 是  false 不是
     */
    public boolean isInformation(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }

        if (isAdmin(uid)){
            int count = userMapper.isInformation(uid);
            return count >= 1;
        }else{
            log.error("该用户不是管理员");
            return false;
        }
    }

    /**
     * 根据uid 判断是否为业务员
     *
     * @param uid
     * @return
     */
    public boolean isSale(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }
        if (isAdmin(uid)){
            int count = userMapper.isSale(uid);
            return count >= 1;
        }else{
            log.error("该用户不是管理员");
            return false;
        }
    }

    /**
     * 根据uid 判断是否为总经理
     *
     * @param uid
     * @return
     */
    public boolean isGM(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }
        if (isAdmin(uid)){
            int count = userMapper.isGM(uid);
            return count >= 1;
        }else{
            log.error("该用户不是总经理!");
            return false;
        }
    }

    /**
     * 根据uid 判断是否为HR
     *
     * @param uid
     * @return
     */
    public boolean isHR(Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return false;
        }
        if (isAdmin(uid)){
            int count = userMapper.isHR(uid);
            return count >= 1;
        }else{
            log.error("该用户不是管理员");
            return false;
        }
    }

}
